package com.xuecheng.search.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xuecheng.api.search.SearchControllerApi;
import com.xuecheng.framework.domain.course.CoursePub;
import com.xuecheng.framework.domain.course.TeachplanMediaPub;
import com.xuecheng.framework.domain.search.CourseSearchParam;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.search.service.SearchService;

@RestController
@RequestMapping("/search/course")
public class SearchController implements SearchControllerApi {

    @Autowired
    SearchService searchService;

    @Override
    @GetMapping("/list/{page}/{size}")
    public QueryResponseResult<CoursePub> list(@PathVariable("page") int page, 
            @PathVariable("size") int size, CourseSearchParam courseSearchParam)
            throws IOException {
        return searchService.list(page,size,courseSearchParam);
    }

    @Override
    @GetMapping("/getall/{id}")
    public Map<String, CoursePub> getAll(@PathVariable("id") String id) {
        return searchService.getAll(id);
    }

    @Override
    @GetMapping("/getmedia/{teachplanId}")
    public TeachplanMediaPub getMedia(@PathVariable("teachplanId") String teachplanId) {
        //封装id为数组，支持多个查询
        String []teachplanIds = new String[] {teachplanId};
        QueryResponseResult<TeachplanMediaPub> queryResponseResult = searchService.getMedia(teachplanIds);
        QueryResult<TeachplanMediaPub> queryResult = queryResponseResult.getQueryResult();
        if(queryResult!=null) {
            List<TeachplanMediaPub> list = queryResult.getList();
            if(list!=null && list.size()>0) {
                return list.get(0);
            }
        }
        return new TeachplanMediaPub();
    }

}
